class Admin::FeaturesController < ApplicationController
  before_filter :check_admin_authorization
  layout "admin"
  # GET /feature
  # GET /feature.xml
  def index
    @features = Feature.find(:all)

    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render :xml => @features.to_xml }
    end
  end

  # GET /feature/1
  # GET /feature/1.xml
  def show
    @feature = Feature.find(params[:id])

    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @feature.to_xml }
    end
  end

  # GET /feature/new
  def new
    @feature = Feature.new
  end

  # GET /feature/1;edit
  def edit
    @feature = Feature.find(params[:id])
  end

  # POST /feature
  # POST /feature.xml
  def create
    @feature = Feature.new(params[:feature])
    respond_to do |format|
      if @feature.save
        path = "#{RAILS_ROOT}/public/files/features/"
        fp =  path + @feature.id.to_s + ".jpg"
	 File.open(fp ,"wb") do |f|
	   f.write(params[:file].read) if params[:file].size > 0
	   f.close
	end
        flash[:notice] = 'Feature was successfully created.'
        format.html { redirect_to  feature_url(@feature) }
        format.xml  { head :created, :location => feature_url(@feature) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @feature.errors.to_xml }
      end
    end
  end

  # PUT /feature/1
  # PUT /feature/1.xml
  def update
    @feature = Feature.find(params[:id])

    respond_to do |format|
      if @feature.update_attributes(params[:feature])
        path = "#{RAILS_ROOT}/public/files/features/"
        fp =  path + @feature.id.to_s + ".jpg"
        if params[:file].size > 0 
	 File.open(fp ,"wb") do |f|
	   f.write(params[:file].read)
	   f.close
	end
	end
        flash[:notice] = 'Feature was successfully updated.'
        format.html { redirect_to  feature_url(@feature) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @feature.errors.to_xml }
      end
    end
  end

  # DELETE /feature/1
  # DELETE /feature/1.xml
  def destroy
    @feature = Feature.find(params[:id])
    @feature.destroy

    respond_to do |format|
      format.html { redirect_to features_url }
      format.xml  { head :ok }
    end
  end
end
